﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using System.Web.UI.WebControls;
using KPIS.GERP.GHRMS.APP.Classes;
using Telerik.Web.UI;


namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PS_HisTraining : System.Web.UI.UserControl
    {
        private Main main = new Main();
        private SsMng creatby = new SsMng();
        private object _dataItem = null;
        private ITrainingHistoryBLL CreateTrainingHis = new TrainingHistoryBLL();

        protected void Page_Load(object sender, EventArgs e)
        {

            lblEmpSeq.Text = Session["emp_seq"].ToString();

            if (DataItem != null)
            {
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    lblTrainHisId.Text = DataBinder.Eval(DataItem, "TrainHisId").ToString();
                    txtCouseName.Text = DataBinder.Eval(DataItem, "CouseName").ToString();
                    txtInstituteName.Text = DataBinder.Eval(DataItem, "InstituteName").ToString();
                    txtLocationName.Text = DataBinder.Eval(DataItem, "LocationName").ToString();
                    txtDateStart.Value = DataBinder.Eval(DataItem, "StartDate").ToString();
                    txtxDateEnd.Value = DataBinder.Eval(DataItem, "EndDate").ToString();
                    txtRemark.Text = DataBinder.Eval(DataItem, "Remark").ToString();
                }

                //cboInstitution.DataSource = CreateTrainingHis.LoadInstitutes();
                //cboInstitution.DataBind();
                //cboInstitution.Items.Insert(0, new RadComboBoxItem("กรุณาเลือกสถาบันที่จัดอบรม", ""));
            }
        }

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }

        private void ClearControlData()
        {
            txtCouseName.Text = "";
            txtInstituteName.Text = "";
            txtLocationName.Text = "";
            txtDateStart.Value = "";
            txtxDateEnd.Value = "";
            txtRemark.Text = "";
        }

        private bool CheckNullValue()
        {
            bool bOk = true;
            Nullable<DateTime> strStartDate = txtDateStart.GetDateValue();
            Nullable<DateTime> strEndDate = txtxDateEnd.GetDateValue();

            if (txtCouseName.Text == "")
            {
                main.MessageAlert(Page, "กรุณากรอกชื่อหลักสูตร");
                bOk = false;
            }
            else if (txtLocationName.Text == "")
            {
                main.MessageAlert(Page, "กรุณากรอกสถานที่จัดอบรม");
                bOk = false;
            }
            else if (strStartDate == null || strStartDate == DateTime.MinValue || strEndDate == null || strEndDate == DateTime.MinValue)
            {
                main.MessageAlert(Page, "กรุณาเลือกวันที่เริ่มต้น และวันที่สิ้นสุด");
                bOk = false;
            }
            
            return bOk;
        }

        protected void cmdAdd_Click(object sender, EventArgs e)
        {
            int? intTrainHisId = main.ConvertNullInt(lblTrainHisId.Text);
            lblEmpSeq.Text = Session["emp_seq"].ToString();
            string strQueryString = lblEmpSeq.Text;
            string strCouseName = txtCouseName.Text;
            string strInstituteName = txtInstituteName.Text;
            string strLocationName = txtLocationName.Text;
            Nullable< DateTime> strStartDate = txtDateStart.GetDateValue();
            Nullable<DateTime> strEndDate = txtxDateEnd.GetDateValue();
            string strRemark = txtRemark.Text;

            if (!CheckNullValue())
            {
                return;
            }

            bool CheckValueSubmit = CreateTrainingHis.CheckValueSubmit(intTrainHisId);
            if (CheckValueSubmit == true)
            {
                CreateTrainingHis.Insert(strQueryString, strCouseName, strInstituteName, strLocationName, strStartDate,
				strEndDate, strRemark, creatby.EmpSeqI);
				main.MessageAlert(Page, "บันทึกข้อมูลแล้ว");
            }

            ChangeMode(Main.EditMode.View);
        }

        protected void cmdUpdate_Click(object sender, EventArgs e)
        {
            int? intTrainHisId = main.ConvertNullInt(lblTrainHisId.Text);
            lblEmpSeq.Text = Session["emp_seq"].ToString();
            string strQueryString = lblEmpSeq.Text;
            string strCouseName = txtCouseName.Text;
            string strInstituteName = txtInstituteName.Text;
            string strLocationName = txtLocationName.Text;
            Nullable<DateTime> strStartDate = txtDateStart.GetDateValue();
            Nullable<DateTime> strEndDate = txtxDateEnd.GetDateValue();
            string strRemark = txtRemark.Text;
            if (!CheckNullValue())
            {
                return;
            }
            bool CheckValueSubmit = CreateTrainingHis.CheckValueSubmit(intTrainHisId);
             if (CheckValueSubmit == true)
             {
                 CreateTrainingHis.Update(strQueryString, strCouseName, strInstituteName, strLocationName, strStartDate,
				strEndDate, strRemark, creatby.EmpSeqI, intTrainHisId);
				 main.MessageAlert(Page, "บันทึกข้อมูลแล้ว");
             }

            ChangeMode(Main.EditMode.View);
        }

        protected void btnCancle_Click(object sender, EventArgs e)
        {
           
        }
        
        private void ChangeMode(Main.EditMode mode)
        {
            switch (mode)
            {
               case Main.EditMode.AddNew:
                    cmdUpdate.Visible = false;
                    cmdAdd.Visible = true;
                    cmdCancel.Visible = true;

                    break;

                case Main.EditMode.View:
                    cmdAdd.Visible = false;
                    cmdUpdate.Visible = false;
                    cmdCancel.Visible = true;
                    break;

                case Main.EditMode.Edit:
                    cmdAdd.Visible = false;
                    cmdCancel.Visible = true;
                    cmdUpdate.Visible = true;
                    break;
            }
        }
  }
}